﻿namespace UI
{
    using System;
    using System.Windows.Forms;
    using Common.EventArg;
    using Common.Helpers;
    using Common.Localization;
    using Common.Protocol;

    public partial class Chart : UserControl, ILocalizable
    {
        private ProtocolInterval interval;

        public event EventHandler<DateTimeEventArgs> DateTimeChanged;

        public event EventHandler<DateTimeEventArgs> RedLinePositionChanged;

        public event EventHandler CenterCharts;

        public event EventHandler<ChartScaleEventArgs> ScaleChanged;

        public Chart()
        {
            this.InitializeComponent();

            this.chartCanvas1 = new ChartCanvas(this.Width);
            this.elementHost1.Child = this.chartCanvas1;

            this.navigationPanel1.Enabled = false;
            this.navigationPanel1.DateTimeChanged += this.NavigateToTime;

            this.chartCanvas1.RedLinePositionChanged += this.ChartCanvasRedLinePositionChanged;

            this.navigationPanel1.CenterChart += this.CenterChartEventHandler;
            this.navigationPanel1.ScaleChanged += this.ScaleChangedEventHandler;
            this.navigationPanel1.NavigateDown += this.NavigateDownEventHandler;
            this.navigationPanel1.NavigateUp += this.NavigateUpEventHandler;

            this.chartCanvas1.CurrentTimeChannged += this.NavigateToTime;
        }
        
        private void ChartCanvasRedLinePositionChanged(object sender, DateTimeEventArgs dateTime)
        {
            this.navigationPanel1.SetDateTime(dateTime.Time);

            if (this.RedLinePositionChanged != null)
            {
                this.RedLinePositionChanged(this, dateTime);
            }
        }

        public void Initialize(ProtocolInterval interval)
        {
            this.interval = interval;
            this.chartCanvas1.Initialize(interval);
            this.navigationPanel1.Enabled = true;
            this.navigationPanel1.SetDateTime(interval.CurrentTime);
        }

        public void ResetChannels()
        {
            this.chartCanvas1.ResetChannels();
        }

        #region navigation panel event handlers

        public void NavigateToTime(object sender, DateTimeEventArgs e)
        {
            if (this.DateTimeChanged != null)
            {
                this.DateTimeChanged(this, e);
            }
        }

        private void NavigateUpEventHandler(object sender, EventArgs e)
        {
            int shiftMilliseconds = (int)(this.interval.TotalMilliseconds - this.interval.StepMilliseconds);
            DateTime newTime = TimeCounter.AddMilliseconds(this.interval.CurrentTime, shiftMilliseconds);
            this.NavigateToTime(this, new DateTimeEventArgs(newTime));
        }

        private void NavigateDownEventHandler(object sender, EventArgs e)
        {
            int shiftMilliseconds = (int)(this.interval.TotalMilliseconds - this.interval.StepMilliseconds);
            DateTime newTime = TimeCounter.RemoveMilliseconds(this.interval.CurrentTime, shiftMilliseconds);
            this.NavigateToTime(this, new DateTimeEventArgs(newTime));
        }

        private void ScaleChangedEventHandler(object sender, ChartScaleEventArgs e)
        {
            if (this.ScaleChanged != null)
            {
                this.ScaleChanged(this, e);
            }
        }

        private void CenterChartEventHandler(object sender, EventArgs e)
        {
            if (this.CenterCharts != null)
            {
                this.CenterCharts(this, null);
            }
        }

        #endregion

        public void Localize()
        {
            this.navigationPanel1.Localize();
        }

        public void SetNavigationPanelTime(DateTime time)
        {
            this.navigationPanel1.SetDateTime(time);
        }
    }
}